package com.tedu.dao.impl;

import com.tedu.dao.DeptDao;
import com.tedu.entity.Dept;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class DeptDaoImpl implements DeptDao {
    @Override
    public List<Dept> findAllDept() {
        ArrayList<Dept> depts = new ArrayList<>();
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/ankang",
                    "root",
                    "root"
            );
            String sql = "select * from dept";
            PreparedStatement pstm = conn.prepareStatement(sql);
            ResultSet rs = pstm.executeQuery();
            while (rs.next()) {
                depts.add(
                        new Dept(
                        rs.getInt(1),
                        rs.getString(2))
                );
            }
            rs.close();
            pstm.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return depts;
    }
}
